package com.study.other1;

import java.util.List;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
 * @Author tanglingyu
 * @Date: 2021/08/04/ 14:40
 * @Description
 */
@Mapper
public interface PersonConverter {

  PersonConverter INSTANCE = Mappers.getMapper(PersonConverter.class);

  @Mappings({
      @Mapping(source = "birthday", target = "birth"),
      @Mapping(source = "birthday", target = "birthDateFormat", dateFormat = "yyyy-MM-dd HH:mm:ss"),
      @Mapping(target = "birthExpressionFormat", expression = "java(org.apache.commons.lang3.time.DateFormatUtils.format(person.getBirthday(),\"yyyy-MM-dd HH:mm:ss\"))"),
      @Mapping(source = "user.age", target = "age"),
      @Mapping(target = "email", ignore = true)
  })
  PersonDTO domain2dto(Person person);

  default List<PersonDTO> domain2dto(List<Person> people) {
    return null;
  }

  // 形式如下
  default PersonDTO personToPersonDTO(Person person) {
    //hand-written mapping logic
    return new PersonDTO();
  }

  // 比如在 PersonConverter 里面加入如下
  default Boolean convert2Bool(Integer value) {
    if (value == null || value < 1) {
      return Boolean.FALSE;
    } else {
      return Boolean.TRUE;
    }
  }

  default Integer convert2Int(Boolean value) {
    if (value == null) {
      return null;
    }
    if (Boolean.TRUE.equals(value)) {
      return 1;
    }
    return 0;
  }

  // 比如在 PersonConverter 里面加入如下，@InheritConfiguration 用于继承刚才的配置
  @InheritConfiguration(name = "domain2dto")
  void update(Person person, @MappingTarget PersonDTO personDTO);
}
